﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace sqlsearcher
{
    public class EnumHelper
    {
        /// <summary>
        /// 返回指定枚举类型的指定值的描述
        /// </summary>
        /// <param name="t">枚举类型</param>
        /// <param name="v">枚举值</param>
        /// <returns></returns>
        private static string GetDescription(System.Type t, object v,string defaultVal)
        {
            try
            {
                if (!Enum.IsDefined(t, v))
                    return defaultVal;
                FieldInfo fi = t.GetField(GetName(t, v));
                DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
                return (attributes.Length > 0) ? attributes[0].Description : GetName(t, v);
            }
            catch
            {
                return defaultVal;
            }
        }

        private static string GetName(System.Type t, object v)
        {
            try
            {
                return t.GetEnumName(v);
            }
            catch (Exception ex)
            {
                return "";
            }
        }


        public static Dictionary<int,string> GetStatus(System.Type t, int[] dels = null)
        {
            Dictionary<int, string> list = new Dictionary<int, string>();

            Array a = Enum.GetValues(t);
            for (int i = 0; i < a.Length; i++)
            {
                string enumName = a.GetValue(i).ToString();
                int enumKey = (int)System.Enum.Parse(t, enumName);
                string enumDescription = GetDescription(t, enumKey, "");
                if (dels != null && dels.Contains(enumKey))
                {
                    continue;
                }

                list.Add(enumKey, enumDescription);

            }

            return list;
        }

        public static Dictionary<int, string> GetStatus<T>(int[] dels)
        {
            return GetStatus(typeof(T), dels);
        }

        public static Dictionary<int, string> GetStatus<T>(int del)
        {
            return GetStatus(typeof(T), new int[] { del });
        }

        public static Dictionary<int, string> GetStatus<T>()
        {
            return GetStatus(typeof(T), null);
        }
        public static string GetDescription<T>(object v,string defaultVal="")
        {
            return GetDescription(typeof(T), v, defaultVal);

        }

        public static T GetEnumByDesc<T>(string desc)
        {
            T t = default(T);
            try
            {
                var dics = EnumHelper.GetStatus<T>();
                foreach (var item in dics)
                {
                    if (item.Value == desc)
                    {
                        t = (T)Enum.Parse(typeof(T), item.Key.ToString());
                    }
                }
            }
            catch (Exception ex)
            {

            }

            return t;
        }

    }
}
